package com.beyond.trace.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.beyond.trace.domain.FruitManage;
import com.beyond.trace.mapper.FruitManageMapper;
import com.beyond.trace.service.FruitManageService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: FruitManageServiceImpl
 * @author: z越
 * @date: 2024/4/9  9:16
 * @description
 */
@Service
@Slf4j
public class FruitManageServiceImpl extends ServiceImpl<FruitManageMapper, FruitManage> implements FruitManageService {
    private final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    @Override
    public void insertData(){
        String[] urls = {"https://www.cnhnb.com/hangqing/cdlist-2002033-0-23-0-0-1/"};
        List<FruitManage> list = new ArrayList<>();
        for (String url : urls) {
            list.addAll(priceTask(url));
        }
        if (CollectionUtils.isNotEmpty(list)) {
            this.saveBatch(list);
        }
    }

    private List<FruitManage> priceTask(String url) {
        //网站上获取的token
        List<FruitManage> list = new ArrayList<>();
        String token = "fingerId=ab82e18b0eef5b39d2c088727223819e; " +
                "deviceId=33d726c-6f1e-483f-8c37-cefc86bb9; " +
                "sessionId=S_0LUQIZ2VBLF67OWV; Hm_lvt_91cf34f62b9bedb16460ca36cf192f4c=1712554906; " +
                "Hm_lvt_b99541cbfb0edd202bb49abf3a0bef84=1712554906; Hm_lvt_0e023fed85d2150e7d419b5b1f2e7c0f=1712555339;" +
                " Hm_lpvt_0e023fed85d2150e7d419b5b1f2e7c0f=1712555339; Hm_lvt_a6458082fb548e5ca7ff77d177d2d88d=1712555340;" +
                " Hm_lpvt_a6458082fb548e5ca7ff77d177d2d88d=1712555340; Hm_lpvt_91cf34f62b9bedb16460ca36cf192f4c=1712560799;" +
                " Hm_lpvt_b99541cbfb0edd202bb49abf3a0bef84=1712560799";
        try {
            Document document = Jsoup.connect(url)
                    .userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36").header("Accept-Language", "zh-CN,zh;q=0.9")
                    .header("Cookie", token).ignoreHttpErrors(true).get();
            if (!checkResponse(document)) {
                Thread.sleep(10 * 60 * 1000);
                list = priceTask(url);
            }
            Elements elementsByClass = document.getElementsByAttributeValue("class", "quotation-content-list");
            if (elementsByClass.size() > 0) {
                for (Element element : elementsByClass) {
                    Elements tr = element.getElementsByTag("span");
                    if (!tr.isEmpty()) {
                        int num = tr.size() / 6;

                        for (int i = 0; i < num; i++) {
                            int time = (6 * i);
                            int name = 1 + (6 * i);
                            int address = 2 + (6 * i);
                            int price = 3 + (6 * i);
                            Element tE = tr.get(time);
                            Element tN = tr.get(name);
                            Element tA = tr.get(address);
                            Element tP = tr.get(price);
                            list.add(new FruitManage(tN.text(),sdf.parse(tE.text()),tP.text(),tA.text()));
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    private Boolean checkResponse(Document document) {
        Elements title = document.select("title");
        String text = title.text();
        log.info("爬取到数据title为："+text);
        return !text.contains("请验证");
    }
}
